package com.lenovo.lcdm.common.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lenovo.lcdm.common.enums.ResponseMsgEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Common Response DTO
 *
 * @param <T>
 * @author
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ResponseVo<T> {
    private int code;
    private String message;
    private T data;
    private Long totalCount;

    public ResponseVo(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseVo(ResponseMsgEnum response, T data) {
        this.code = response.getCode();
        this.message = response.getMessage();
        this.data = data;
    }

    public static <T> ResponseVo<T> success() {
        return success(null);
    }

    /**
     * 成功
     */
    public static <T> ResponseVo<T> success(T data) {
        ResponseVo<T> result = new ResponseVo<T>();
        result.setCode(ResponseMsgEnum.SUCCESS.getCode());
        result.setMessage(ResponseMsgEnum.SUCCESS.getMessage());
        result.setData(data);
        if (data instanceof IPage) {
            result.setTotalCount(((IPage) data).getTotal());
        }
        return result;
    }

    /**
     * 失败
     */
    public static <T> ResponseVo<T> error(int code, String message) {
        return new ResponseVo(code, message);
    }

    /**
     * 失败
     */
    public static <T> ResponseVo<T> error(ResponseMsgEnum responseMsgEnum) {
        return new ResponseVo(responseMsgEnum.getCode(), responseMsgEnum.getMessage());
    }
}
